﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domaine;
using System.Text.RegularExpressions;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Configuration;

namespace Persistance
{
    public static class Utils
    {
        /// <summary>
        /// vrai si l'adresse mail est bien construite
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        public static bool ValidMail(string mail)
        {
            Regex myRegex = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", RegexOptions.IgnoreCase);
            return myRegex.IsMatch(mail);
        }

        /// <summary>
        /// vrai si le numero de telephone est bien construit
        /// </summary>
        /// <param name="mobilePhone"></param>
        /// <returns></returns>
        public static bool ValidMobilePhone(string mobilePhone)
        {
            Regex myRegex = new Regex(@"^[0-9]{10}$", RegexOptions.IgnoreCase);
            return myRegex.IsMatch(mobilePhone);
        }

        /// <summary>
        /// vrai si l'url est bien construite
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool ValidURL(string url)
        {
            Regex myRegex = new Regex(@"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$", RegexOptions.IgnoreCase);
            return myRegex.IsMatch(url);
        }

        /// <summary>
        /// renvoie vrai si le collab est dans l'equipe de CP
        /// </summary>
        /// <param name="CP"></param>
        /// <param name="collab"></param>
        /// <returns></returns>
        public static bool CollabAssociateToCP(Collaborator CP, Collaborator collab)
        {
            var projectsCP = CP.ProjectSet.Select(p => p.State.ID != (int)StateEnum.Termine);
            var projectsCollab = collab.ProjectSet.Select(p => p.State.ID != (int)StateEnum.Termine);
            return projectsCollab.Intersect(projectsCP).Count() > 0;
        }

        /// <summary>
        /// renvoie vrai si le collab est sous la responsabilité du RA
        /// </summary>
        /// <param name="RA"></param>
        /// <param name="collab"></param>
        /// <returns></returns>
        public static bool CollabAssociateToRA(Collaborator RA, Collaborator collab)
        {
            bool isRA = (RA.Job.ID == (int)JobEnum.RA);
            bool areAssociated = (RA.Location.ID == collab.Location.ID) && (RA.Activity.ID == collab.Activity.ID);
            return isRA && areAssociated;
        }

        /// <summary>
        /// sauvegarde une image dans un blob et renvoie l'uri pour y accéder
        /// </summary>
        /// <param name="byteArrayIn"></param>
        /// <param name="login"></param>
        /// <param name="container"></param>
        /// <returns></returns>
        public static string SaveImage(byte[] byteArrayIn, String login, String container, DateTime date)
        {
            CloudStorageAccount cloudStorageAccount;
            CloudBlobClient blobClient;
            CloudBlobContainer blobContainer;

            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(ConfigurationManager.AppSettings[configName]); });

            //cloudstorageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString"));
            cloudStorageAccount = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");

            //cloudStorageAccount.BlobEndpoint=""
            // Créer un blob client avec l'authentification 
            blobClient = cloudStorageAccount.CreateCloudBlobClient();

            blobContainer = blobClient.GetContainerReference(container);

            if (blobContainer.CreateIfNotExist())
            {
                blobContainer.SetPermissions(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
            }

            var azureImage = blobContainer.GetBlockBlobReference(login + ".png");
            azureImage.DeleteIfExists();
            azureImage.Properties.ContentType = "image/png";
            //azureImage.Properties.LastModifiedUtc = date;
            azureImage.UploadByteArray(byteArrayIn);

            return azureImage.Uri.ToString();
        }
    }
}
